package game

import (
	"fmt"
	"server/MergeConst"
	"server/db"
	"server/msg"
	"time"

	"google.golang.org/protobuf/proto"
)

type PlayerPigData struct {
	*PlayerData
	Data msg.ResPigDetailInfo
}

func (p *PlayerPigData) LoadDataFromDB(dwUin interface{}) bool {

	sqlStr := "SELECT * FROM t_player_piginfo WHERE dwUin = ?"
	sqlStruck := db.SqlPigDetailInfoStruct{}
	sqlStruck.PigId = 0
	if err := db.SqlDb.Get(&sqlStruck, sqlStr, dwUin.(int32)); err != nil {
		fmt.Printf("get data failed, err:%v\n", err)
		p.IsHaveDataDb = false
	} else {
		p.IsHaveDataDb = true
	}
	p.Data.CurDiamonds = sqlStruck.CurDiamonds
	p.Data.DwUin = p.M_Player.M_DwUin
	p.Data.PigId = sqlStruck.PigId
	p.Data.PigType = sqlStruck.PigType
	p.Data.StartSvrTime = sqlStruck.StartSvrTime
	p.Data.EndSvrTime = sqlStruck.EndSvrTime
	p.Data.CurGetLv = sqlStruck.CurGetLv
	p.Reconnect(false)

	return true
}
func (p *PlayerPigData) Reconnect(b bool) {

}

func (p *PlayerPigData) DeleteLastPigData(pigid int32) {
	sqlStr := "DELETE FROM " + "t_player_piginfo" + " WHERE PigId = ?"

	db.SqlDb.Exec(sqlStr, pigid)
	p.Data.PigId = 0
}

func (p *PlayerPigData) ClearData() bool {
	p.SaveDataFromDB("")

	return true
}
func (p *PlayerPigData) SaveDataFromDB(Key interface{}) bool {

	sqlStruck := db.SqlPigDetailInfoStruct{}
	sqlStruck.DwUin = p.M_Player.M_DwUin
	sqlStruck.CurDiamonds = p.Data.CurDiamonds
	sqlStruck.CurGetLv = p.Data.CurGetLv
	sqlStruck.CurSvrTime = 0
	sqlStruck.EndSvrTime = p.Data.EndSvrTime
	sqlStruck.StartSvrTime = p.Data.StartSvrTime
	sqlStruck.PigId = p.Data.PigId
	sqlStruck.PigType = p.Data.PigType

	if p.IsHaveDataDb {
		if p.Data.PigId != 0 {
			db.FormatAllMemUpdateDb(&sqlStruck, "t_player_piginfo", "PigId")
		}
	} else {
		if p.Data.PigId != 0 {
			db.FormatAllMemInsertDb(&sqlStruck, "t_player_piginfo")
		}
	}
	p.IsHaveDataDb = true
	return true
}

func (p *PlayerPigData) ResOpenNewPig(player *Player) {
	res := &msg.ResOpenNewPig{}
	st := &db.SqlPigDetailInfoStruct{}
	st.DwUin = player.M_DwUin
	st.StartSvrTime = (int32)(time.Now().Unix())
	st.EndSvrTime = st.StartSvrTime + MergeConst.G_Pig_Limite
	st.PigType = 1
	st.CurDiamonds = 0
	st.CurSvrTime = 0
	st.CurGetLv = 1

	PigId, _ := db.FormatAllMemInsertDb(st, "t_player_piginfo")
	res.PigId = (int32)(PigId)

	p.Data.CurDiamonds = st.CurDiamonds
	p.Data.DwUin = p.M_Player.M_DwUin
	p.Data.PigId = int32(PigId)
	p.Data.PigType = st.PigType
	p.Data.StartSvrTime = st.StartSvrTime
	p.Data.EndSvrTime = st.EndSvrTime

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResOpenNewPig", data)
}

func (p *PlayerPigData) ResPigDetailInfo(player *Player) {
	// res := &msg.ReqPigDetailInfo{}
	p.Data.CurSvrTime = int32(time.Now().Unix())
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(&p.Data)
	G_getGameLogic().PackResInfo(agent, "ResPigDetailInfo", data)
}

func (p *PlayerPigData) UpdateFinishOrderDiamond(buf []byte) {

	update := &msg.UpdateFinishOrderDiamond{}
	proto.Unmarshal(buf, update)

	p.Data.CurDiamonds = update.CurDiamonds
	p.SaveDataFromDB("")
}

func (p *PlayerPigData) ResGetPigReward(buf []byte) {

	req := &msg.ReqGetPigReward{}
	proto.Unmarshal(buf, req)

	res := &msg.ResGetPigReward{}
	res.PigId = req.PigId

	if req.PigId != p.Data.PigId {
		res.ResultCode = MergeConst.Protocol_PigID_No_Exsit
	} else {
		res.ResultCode = 0
		p.Data.CurGetLv = p.Data.CurGetLv + 1
		res.CurGetLv = p.Data.CurGetLv
	}
	res.CurDiamonds = p.Data.CurDiamonds

	if MergeConst.G_PigIsOne {
		p.DeleteLastPigData(req.PigId)
	}

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResGetPigReward", data)
}

func (p *PlayerPigData) ResDropPigReward(buf []byte) {

	req := &msg.ReqDropPigReward{}
	proto.Unmarshal(buf, req)

	res := &msg.ResDropPigReward{}
	res.PigId = req.PigId

	if req.PigId != p.Data.PigId {
		res.ResultCode = MergeConst.Protocol_PigID_No_Exsit
	} else {
		res.ResultCode = 0
	}

	p.DeleteLastPigData(req.PigId)

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResDropPigReward", data)
}
